import { h, onMounted, reactive, ref, toRaw } from "vue";
import type { PaginationProps } from "@pureadmin/table";
import { deviceDetection } from "@pureadmin/utils";

import {
  tallyListApi,
  typeSelect
} from "@/api/projectApi";

export function useCorps() {
  const form = reactive({
    page: 1,
    perPage: 20,
    projectName: "",
    status: null,
    projectNo: null,
    dateRange: null,
    projectType: null,
    typeOption: [
      {value: 0, name: '未开始'},
      {value: 1, name: '进行中'},
      {value: 2, name: '已挂起'},
      {value: 3, name: '已完成'},
      {value: 4, name: '已关闭'},
    ],
    projectSelect: [],
    projectTypeOptions: [
      {id:1,name: '生产'},
      {id:2,name: '生产实施'},
      {id:3,name: '集成(有生产)'},
      {id:4,name: '集成(无生产)'},
      {id:5,name: '外包'},
      {id:6,name: '贸易'},
    ],
    taskType: null,
    taskTypeOptions: [],
    timeType: null,
    rangeDate: null,
    electControl: null,
    consignType: null,
    isDelive: null,
    receiptFull: null
  });
  const dataList = ref([]);
  const isShow = ref(false);
  const loading = ref(true);
  const higherDeptOptions = ref();
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 20,
    currentPage: 1,
    background: true,
    pageSizes: [10,20,50,100]
  });
  const columns: TableColumnList = [
    // {
    //   label: "序号",
    //   type: "index",
    //   index: indexMethod,
    //   // width: 60
    // },
    { type: "expand", slot: "expand", width: 30,fixed: "left",},
    {
      label: "项目名称",
      prop: "projectName",
      width: 200,
      fixed: "left",
    },
    {
      label: "项目编号",
      prop: "projectNo",
      minWidth: 150,
      sortable: true,
      // fixed: "left",
    },
    {
      label: "任务阶段",
      prop: "taskTypeName",
      minWidth: 110,
      slot: 'taskTypeName'
    },
    {
      label: "剩余时间(天)",
      prop: "surplusDate",
      slot: "surplusDate",
      minWidth: 110
    },
    {
      label: "任务总数",
      prop: "taskCount",
      minWidth: 100
    },
    {
      label: "交付状态",
      prop: "isDelive",
      slot: "isDelive",
      minWidth: 110,
    },
    {
      label: "逾期任务数",
      prop: "overdueTaskCount",
      minWidth: 100
    },
    {
      label: "项目类型",
      prop: "projectType",
      slot: "projectType",
      minWidth: 100
    },
    {
      label: "货款类型",
      prop: "consignType",
      formatter:(row: any)=> {
        return row.consignType === 1? '发货款' : '到货款';
      },
      minWidth: 100
    },
    {
      label: "是否电控",
      prop: "electControl",
      formatter:(row: any)=> {
        return row.electControl === 1? '是' : '否';
      },
      minWidth: 100
    },
    {
      label: "实收款(%)",
      prop: "realReceiptRatio",
      minWidth: 90,
      formatter:(row: any)=> {
        return row.realReceiptRatio >= 100?100:row.realReceiptRatio
      },
    },
    {
      label: "预收款(%)",
      prop: "receiptRatio",
      minWidth: 90,
      formatter:(row: any)=> {
        return row.receiptRatio >= 100?100:row.receiptRatio
      },
    },
    {
      label: "状态",
      prop: "statusName",
      slot: "statusName",
      minWidth: 90
    },
    {
      label: "合同日期",
      prop: "startDate",
      formatter:(row: any)=> {
        return row.startDate + '至'+ row.endDate
      },
      minWidth: 190
    },
    {
      label: "计划日期",
      prop: "planStartDate",
      formatter:(row: any)=> {
        return row.planStartDate + '至'+ row.planEndDate
      },
      minWidth: 190
    },
    {
      label: "工作日(天)",
      prop: "effectDay",
      minWidth: 90
    },
    {
      label: "收款标识",
      prop: "receiptFull",
      slot: "receiptFull",
      minWidth: 110,
    },
    {
      label: "负责人",
      prop: "principalUser.name",
      minWidth: 100
    },
    {
      label: "创建人",
      prop: "createUser.name",
      minWidth: 100
    },
    {
      label: "创建时间",
      prop: "createdAt",
      minWidth: 160
    },
    {
      label: "更新时间",
      prop: "updatedAt",
      minWidth: 160
    },

  ];
  const expandColumns: TableColumnList = [
    {
      label: "任务阶段",
      prop: "taskTypeName",
      width: 170,
      fixed: "left",
    },
    {
      label: "状态",
      prop: "status",
      slot: "status",
      width: 90
    },
    {
      label: "计划日期",
      prop: "planStartDate",
      formatter:(row: any)=> {
        return (row.planStartDate ?? '--') + '至'+ (row.planEndDate ?? '--')
      },
      minWidth: 220
    },
    {
      label: "实际日期",
      prop: "planStartDate",
      formatter:(row: any)=> {
        return (row.realStartDate ?? '--') + '至'+ (row.realEndDate ?? '--')
      },
      minWidth: 220
    },

    {
      label: "完成比例(%)",
      prop: "completeRate",
      minWidth: 110,
      formatter:(row: any)=> {
        let str: any = 0;
        if(row.completeRate) {
          str = row.completeRate*1
        }
        return str
      },
    },
    {
      label: "收款比例(%)",
      prop: "receiptRatio",
      minWidth: 110,
      formatter:(row: any)=> {
        let str: any = '--';
        if(row.taskType === 1020000 || row.taskType === 1070000 || row.taskType === 1090000 || row.taskType === 1120000 || row.taskType === 1130000) {
          str = row.receiptRatio
        }
        return str
      },
    },
    {
      label: "过期时间(天)",
      prop: "overdueDate",
      minWidth: 110,
      formatter:(row: any)=> {
        let str: any = row.overdueDate ?? '--';
        return str
      },
    },
    {
      label: "预计小时",
      prop: "expectHour",
      width: 90,
      formatter:(row: any)=> {
        return row.expectHour || '--'
      },
    },
    {
      label: "耗时(H)",
      prop: "expendHour",
      width: 90,
      formatter:(row: any)=> {
        return row.expendHour || '--'
      },
    },
    {
      label: "指派给用户",
      prop: "assigneds",
      minWidth: 120,
      slot: "assigneds"
    },
    {
      label: "抄送给用户",
      prop: "assignedExtraTo",
      minWidth: 120,
      slot: "assignedExtraTo"
    },
  ]
  function handleSizeChange(val: number) {
    loading.value = true;
    form.page = 1;
    form.perPage = val;
    onSearch();
  }

  function handleCurrentChange(val: number) {
    loading.value = true;
    form.page = val;
    onSearch();
  }

  async function onSearch() {
    loading.value = true;
    let params = {
      page: form.page,
      perPage: form.perPage,
      projectName: form.projectName,
      projectNo: form.projectNo,
      projectType: form.projectType,
      taskType: form.taskType,
      status: form.status,
      timeType: form.timeType,
      startTime: form.rangeDate ? form.rangeDate[0] : null,
      endTime: form.rangeDate ? form.rangeDate[1] : null,
      electControl: form.electControl,
      consignType: form.consignType,
      isDelive: form.isDelive,
      receiptFull: form.receiptFull,
    }
    const res: any = await tallyListApi(toRaw(params));
    dataList.value = res?.result.list;
    pagination.total = res?.result.total;
    pagination.pageSize = res?.result.perPage*1;
    pagination.currentPage = res?.result.page*1;

    setTimeout(() => {
      loading.value = false;
    }, 150);
  }

  const resetForm = formEl => {
    if (!formEl) return;
    form.page = 1;
    form.projectName = null;
    form.projectNo = null;
    form.projectType = null;
    form.taskType = null;
    form.status = null;
    form.timeType = null;
    form.rangeDate = null;
    form.electControl = null;
    form.consignType = null;
    form.isDelive = null;
    form.receiptFull = null;
    formEl.resetFields();
    onSearch();
  };

  onMounted(async () => {
    onSearch();
    getTypeSelect()
  });
  // 类型列表
  const getTypeSelect = async() => {
    const res: any = await typeSelect()
    form.taskTypeOptions = res?.result ?? []
  }
  return {
    form,
    isShow,
    loading,
    columns,
    dataList,
    pagination,
    onSearch,
    resetForm,
    handleSizeChange,
    handleCurrentChange,
    deviceDetection,
    expandColumns
  };
}
